import urls from '/util/urls'
import request from '/util/request'
import { parseTime } from '/util/utils'
const app = getApp()

Page({
  data: {
    // 任务列表
    taskList: [],
    // 类型
    type: '',
    query: ''
  },
  onLoad(query) {
    this.query = query
    request.login(this.init.bind(this))
  },
  init() {
    let that = this
    const type = this.query.type
    // 类型
    that.setData({
      type: type
    })
    // 加载当前用户的每日巡检任务
    if (type == 'day') {
      dd.setNavigationBar({
        title: '每日巡检'
      })
      request.get(urls.inspectionDayListUrl(['notStarted', 'inspecting']), function (data) {
        that.setData({
          taskList: data.map(it => {
            return {
              id: it.id,
              institutionName: it.institutionName,
              startDateTime: parseTime(it.startDateTime, '{h}:{i}'),
              endDateTime: parseTime(it.endDateTime, '{h}:{i}'),
              time: parseTime(it.startDateTime, '{m}-{d}')
            }
          })
        })
      })
    }
    // 定期巡检
    else if (type == 'regular') {
      dd.setNavigationBar({
        title: '定期巡检'
      })
      request.get(urls.inspectionRegularListUrl(['notStarted', 'inspecting', 'upload']), function (data) {
        that.setData({
          taskList: data.map(it => {
            return {
              id: it.id,
              institutionName: it.institutionName,
              startDateTime: parseTime(it.startDateTime, '{h}:{i}'),
              endDateTime: parseTime(it.endDateTime, '{h}:{i}'),
              time: parseTime(it.startDateTime, '{m}-{d}'),
              status: it.status
            }
          })
        })
      })
    }
    // 重保巡检
    else if (type == 'insurance') {
      dd.setNavigationBar({
        title: '重保服务'
      })
      request.get(urls.inspectionServiceUnfinishedTaskUrl(), function (data) {
        that.setData({
          taskList: data.map(it => {
            return {
              id: it.id,
              institutionName: it.unitName,
              startDateTime: parseTime(it.startTime, '{d}/{m}'),
              endDateTime: parseTime(it.endTime, '{d}/{m}'),
              time: parseTime(it.startTime, '{m}-{d}') + "~" + parseTime(it.endTime, '{m}-{d}')
            }
          })
        })
      })
    }
  },
  // 点击待办任务进入详细
  onItemClick(e) {
    const that = this
    const id = e.target.dataset.id
    // 每日巡检
    if (this.data.type == 'day') {
      dd.redirectTo({
        url: '/pages/inspection/day/day?id=' + id
      })
    }
    // 定期巡检
    else if (this.data.type == 'regular') {
      const one = that.data.taskList.find(s => s.id == id)
      // let items = []
      if (one) {
        if (one.status == 'upload') {
          // items = ['上传附件']
          dd.redirectTo({
            url: '/pages/inspection/regular/attachment/attachment?id=' + id
          })
        } else if (one.status == 'notStarted' || one.status == 'inspecting') {
          // items = ['填写巡检内容']
          dd.redirectTo({
            url: '/pages/inspection/regular/regular?id=' + id
          })
        }
      }
    }
    // 重保巡检
    else if (this.data.type == 'insurance') {
      var pages = getCurrentPages() // 获取页面栈
      const isExist = pages.filter(s => s.query && s.query.type === 'insurance')
      if(isExist.length > 1){
        dd.redirectTo({
          url: '/pages/inspection/insurance/insurance?id=' + id
        })
      }else{
        dd.navigateTo({
          url: '/pages/inspection/insurance/insurance?id=' + id
        })
      }
    }

  }
});
